package com.example.springboot_system.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot_system.mapper.*;
import com.example.springboot_system.pojo.*;
import com.example.springboot_system.service.TakingDrugService;
import com.example.springboot_system.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Service
public class TakingDrugImpl implements TakingDrugService {
    @Autowired(required = false)
    private TollTakeDrugMapper tollTakeDrugMapper;

    @Autowired(required = false)
    private RegisterMapper registerMapper;

    @Autowired(required = false)
    private PatientMapper patientMapper;

    @Autowired(required = false)
    private MedicalRecordMapper medicalRecordMapper;

    @Autowired(required = false)
    private OutpatientQueueMapper outpatientQueueMapper;

    @Override
    public ResponseResult getMedicalRecord(String prescriptionNum, HttpSession session) {
        if (prescriptionNum == null || prescriptionNum =="") {
            return new ResponseResult(201, "请输入处方号");
        }
        QueryWrapper<TollTakeDrug> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("prescription_num",prescriptionNum);
        TollTakeDrug tollTakeDrug = tollTakeDrugMapper.selectOne(queryWrapper);

        QueryWrapper<MedicalRecord> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("prescription_num",prescriptionNum);
        MedicalRecord medicalRecord = medicalRecordMapper.selectOne(queryWrapper1);
        if(medicalRecord==null){
            return new ResponseResult(203, "无该处方号");
        }
        /*System.out.println("medicalRecord::::::::"+medicalRecord);*/

        QueryWrapper<Register> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("id",medicalRecord.getRegisterId());
        Register register = registerMapper.selectOne(queryWrapper2);
        /*System.out.println("register::::::::"+register);*/

        QueryWrapper<Patient> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("id",tollTakeDrug.getPatientId());
        Patient patient = patientMapper.selectOne(queryWrapper3);
        /*System.out.println("patient::::::"+patient);*/

        return new ResponseResult(200,"查询成功",medicalRecord,register,patient);
    }

    @Override
    public ResponseResult finishTakeDrug(String prescriptionNum, HttpSession session) {
        if (prescriptionNum == null || prescriptionNum =="") {
            return new ResponseResult(201, "请输入处方号");
        }
        User user = (User) session.getAttribute("user");
        if(user==null){
            return new ResponseResult(202, "会话已超时，请重新登录");
        }

        QueryWrapper<TollTakeDrug> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("prescription_num",prescriptionNum);
        TollTakeDrug tollTakeDrug = tollTakeDrugMapper.selectOne(queryWrapper);
        if(tollTakeDrug.getTakingDrugStatus()==1){
            return new ResponseResult(203, "该处方号已取药");
        }
        Date time = new Date();
        tollTakeDrug.setTakingDrugOperator(tollTakeDrug.getPatientId());
        tollTakeDrug.setTakingDrugDateTime(time);
        tollTakeDrug.setTakingDrugStatus(1);
        tollTakeDrugMapper.updateById(tollTakeDrug);
/*        System.out.println("tollTakeDrug::::::::"+tollTakeDrug);*/

        //根据挂单号id查询出中间表，修改status=1
        QueryWrapper<MedicalRecord> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("prescription_num",prescriptionNum);
        MedicalRecord medicalRecord = medicalRecordMapper.selectOne(queryWrapper1);

        QueryWrapper<OutpatientQueue> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("register_id",medicalRecord.getRegisterId());
        OutpatientQueue outpatientQueue = outpatientQueueMapper.selectOne(queryWrapper2);
        outpatientQueue.setOutpatientQueueStatus(1);
        outpatientQueueMapper.updateById(outpatientQueue);

        Register register = new Register();
        register.setId(medicalRecord.getRegisterId());
        register.setChargeStatus(1);
        registerMapper.updateById(register);
        System.out.println("outpatientQueue::::::::"+outpatientQueue);
        return new ResponseResult(200, "取药成功");
    }
}
